<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    include "../../include/dbFunctions.php";
    function deleteEvent($ID) {
        $id = $ID;
        //delete file
        $query0 = "SELECT class_student_list FROM class WHERE class_id='".$id."'";
        $result0 = executeSELECTQuery($query0);
        $file = "../../upload/".$result0[0]['class_student_list'];
        //header("Location:".$file);
        if($result0[0]['class_student_list']!=null) {
            if (unlink($file)) {
                $_SESSION['mes']='File has been removed sucessfully';
            }
        }
        //delete file

        //delete track detail
        $query2 = "DELETE FROM trainer WHERE event_id= $id";
        $result2 = executeQuery($query2);
        //delete trainer
        $query3 = "DELETE FROM trainer WHERE event_id= $id";
        $result3 = executeQuery($query3);
        //delete class
        $query4 = "DELETE FROM class WHERE event_id= $id";
        $result4 = executeQuery($query4);
        //delete event main
        $query1 = "DELETE FROM event WHERE event_id= $id";
        $result1 = executeQuery($query1);
        return $result1;
    }
    if (isset($_POST['id'])) {
        $id_array=$_POST['id'];
        for($i=0;$i<count($id_array);$i++) {
            $id = $id_array[$i];
            $result1 = deleteEvent($id);
            if ($result1!=0) {
                $_SESSION['mes'] = "Selected courses has been deleted successfully";
            } else {
                $_SESSION['mes'] = "Error! Please try again";
            }
        }
    }
    elseif (isset($_GET['id'])) {
        $id = $_GET['id'];
        //delete trainer
        $result1 = deleteEvent($id);
        if ($result1!=0) {
            $_SESSION['mes']="Selected course has been deleted successfully";
        } else {
            $_SESSION['mes']="Error! Please try again";
        }
    } else {
        $_SESSION['mes'] = "You must choose at least 1 event";
    }


    header('Location: ../eventManageCancel.php');
    ?>
    <?php
}else {
    header("Location: ../include/login.html");
}
?>